package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingListService;
import com.itheima.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/osl")
public class OrderSettingListController {
    @Reference
    private OrderSettingListService orderSettingListService;

    //首先是执行分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody Map map){
        return orderSettingListService.findPage(map);

    }

    @Reference
    private OrderService orderService;

    @Reference
    private MemberService memberService;
    //新增预约数据
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('ORDER_PHONE_ADD')")
    public Result add(@RequestBody Map map, @RequestParam("setmealId") Integer setmealId){
       try {
           //取出封装在map集合中的各项数据
           String name = (String) map.get("name");
           String phoneNumber =(String) map.get("phoneNumber");
           Date orderDate = DateUtils.parseString2Date((String) map.get("orderDate"));
           Member member=memberService.findByName(name);

           //判断该预约用户是否为会员，如果不是的话，那么直接为其注册会员，如果是，那么只需添加预约信息即可
           if(member!=null){
               orderService.addByCondition(orderDate,setmealId,member.getId());
           }

           memberService.addByCondition(name,phoneNumber,orderDate);
           Member member1=memberService.findByName(name);
           orderService.addByCondition(orderDate,setmealId,member1.getId());
       }catch (Exception e){
           e.printStackTrace();
           return new Result(false, MessageConstant.ORDERSETTING_FAIL);
       }
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }

    //根据预约订单的id查询回显数据
    @RequestMapping("/findById")
    public void findById(Integer id){
        System.out.println(id);
        Order order=orderService.findByOrderId(id);
        System.out.println(order.getSetmealId()+"---"+order.getId()+"--"+order.getOrderDate()+"--"+order.getOrderStatus()+"--"+order.getMemberId());
        /*try {
            //根据前端返回的预约订单id回显数据
            Order order=orderService.findByOrderId(id);
            //再根据查询出的order项数据得到member_id
            Member member=memberService.findById(order.getMemberId());

                System.out.println(member.getName()+"---"+member.getId());
            //新建一个集合
            HashMap<String, Object> orderMap = new HashMap<>();
            //将查询出来的数据放入map集合中
            orderMap.put("orderDate",order.getOrderDate());
            orderMap.put("orderType",order.getOrderType());
            orderMap.put("orderStatus",order.getOrderStatus());
            orderMap.put("setmeal_id",order.getSetmealId());
            orderMap.put("name",member.getName());
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,orderMap);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
*/
    }


    //根据返回的id删除数据
    @RequestMapping("/delete")
    public void delete(Integer id) {
        orderService.deleteById(id);
    }

    //根据返回的id对对应的预约订单状态进行修改
    @RequestMapping("/changeStatus")
    public void changeStatus( @RequestParam("id") Integer id){
        orderService.changeStatus(id);
    }

    //修改数据
    @RequestMapping("/edit")
    public Result edit(@RequestBody Map map, @RequestParam("setmealId") Integer setmealId){

        try{
            Integer id =(Integer) map.get("id");
            String orderStatus = (String)map.get("orderStatus");
            String orderType = (String)map.get("orderType");
            String name = (String) map.get("name");
            Date orderDate = DateUtils.parseString2Date((String) map.get("orderDate"));

            orderService.edit(id,orderDate,orderStatus,orderType,setmealId);
            /*Order order = orderService.findByOrderId(id);
            memberService.editByName(name,order.getMemberId());
            System.out.println("------------"+order.getMemberId());*/

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }
}
